package com.topisv.tms.web.stock;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.ExpressStyle;
import com.topisv.tms.entity.User;
import com.topisv.tms.entity.stock.StockBoxCode;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.AccountService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.stock.StockBoxCodeService;
import com.topisv.tms.service.vo.PrinterStyleType;

/**
 * 存储箱条码_control
 * @author ch
 */
@Controller
@RequestMapping(value = "/v2/stockBoxCode")
public class StockBoxCodeController {
	
	private static final Logger logger = LoggerFactory.getLogger(StockBoxCodeController.class);
	
	@Autowired
	private StockBoxCodeService stockBoxCodeService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private CustomersService customersService;
	
	/**
	 * 导向存储箱条码打印页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toStockBoxCodePage")
	public String toStockBoxCodePage(ServletRequest request,HttpServletResponse response,Model model){
		try {
			
			//查找商户组
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			User user=this.accountService.findUserByJobuuid(curUser.jobuuid);
			List<Customers> customerList;
			if(user.getUserType().equals("paiu")){ //派友用户
			    customerList=this.customersService.findAll();
				model.addAttribute("customerList",customerList);
			}else if(user.getUserType().equals("customer")){ //商家用户
				customerList=new ArrayList<Customers>();
				String customerCode=curUser.customerCode;
			    Customers customer=this.customersService.findByCustomerCode(customerCode);
				customerList.add(customer);
				model.addAttribute("customerList",customerList);
				model.addAttribute("customer", customer); 
			}
		
		} catch (Exception e) {
			
			logger.error("error",e);
		}
		
		return "customer/stock/stockBoxCodeMgr";
	}
	
	//查询
	@RequestMapping(value = "ajaxQuery")
	public void ajaxQuery(ServletRequest request,HttpServletResponse response) {
		
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			
			String customerCode=request.getParameter("customerCode");
			String warehouseCode=request.getParameter("warehouseCode");
			
			StockBoxCode stockBoxCode= this.stockBoxCodeService.loadByCondition(customerCode,warehouseCode);
			
			json.put("status", "success");
			json.put("stockBoxCode", stockBoxCode);
			  
		} catch (Exception e) {
			json.put("status", "error");
			
			logger.error("error",e);
		}
		out.print(json.toString());
	}
	
	
	//修改
	@ResponseBody
	@RequestMapping(value = "ajaxUpdateRankno")
	public String ajaxUpdateRankno(ServletRequest request,HttpServletResponse response) {
		try {
			String customerCode=request.getParameter("customerCode");
			String warehouseCode=request.getParameter("warehouseCode");
			int num=Integer.valueOf(request.getParameter("num"));
			this.stockBoxCodeService.updateRankno(customerCode,warehouseCode,num);
			
			return "true";
		} catch (Exception e) {
			
			logger.error("error",e);
			return "false";
		}
	}
	
//	@RequestMapping(value = "ajaxUpdateRankno", method = RequestMethod.POST)
//	public void ajaxUpdateRankno(
//			@RequestParam(value = "customerCode") String customerCode,
//			@RequestParam(value = "warehouseCode") String warehouseCode,
//			@RequestParam(value = "num") int num,
//			HttpServletResponse response) {
//		PrintWriter out=null;
//		JSONObject json=null;
//		try {
//			
//			json=new JSONObject();
//			out=response.getWriter();
//			this.stockBoxCodeService.updateRankno(customerCode,warehouseCode,num);
//			
//			json.put("status", "1");
//
//			out.print(json.toString());
//		} catch (Exception e) {
//			
//			json.put("status", "0");
//			json.put("content", e.getMessage());
//			out.print(json.toString());
//			logger.error("打印加载错误", e);
//		}
//	}
	
	
	
	
	
	
}
